import bpy


import engine.initLIB.load_dll as engineLib
import ctypes


from 物体.物体管理 import engine_物体创建




def f_轮询场景(scene):
	engineLib.flexCAPI.f_安放场景(ctypes.c_uint(scene.m_场景ID))
	#print('scene.m_场景ID = ', scene.m_场景ID)
	#if scene.m_PhysX :
	#	engineLib.flexCAPI.f_创建PhysX(ctypes.c_uint(scene.m_场景ID))
	if scene.m_FleX :
		engineLib.flexCAPI.f_创建FleX(ctypes.c_uint(scene.m_场景ID), ctypes.c_uint(scene.m_FleX_粒子数), ctypes.c_uint(scene.m_FleX_飞溅数))



class C_添加PhysX(bpy.types.Operator):
	bl_idname = 'scene.add_physx'
	bl_label = 'PhysX'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		context.scene.m_PhysX = True
		engineLib.flexCAPI.f_创建PhysX(ctypes.c_uint(context.scene.m_场景ID))
		return {'FINISHED'}
		

class C_添加FleX(bpy.types.Operator):
	bl_idname = 'scene.add_flex'
	bl_label = 'FleX'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		context.scene.m_FleX = True
		engineLib.flexCAPI.f_创建FleX(ctypes.c_uint(context.scene.m_场景ID), ctypes.c_uint(context.scene.m_FleX_粒子数), ctypes.c_uint(context.scene.m_FleX_飞溅数))
		return {'FINISHED'}




class C_移除FleX(bpy.types.Operator):
	bl_idname = 'scene.remove_flex'
	bl_label = '移除FleX'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		context.scene.m_FleX = False
		#engineLib.flexCAPI.f_创建FleX(ctypes.c_uint(context.scene.m_场景ID), ctypes.c_uint(context.scene.m_FleX_粒子数), ctypes.c_uint(context.scene.m_FleX_飞溅数))
		return {'FINISHED'}






classes = (
	C_添加PhysX,
	
	C_添加FleX,
	C_移除FleX,
)




def f_engine_场景更新(scene):
	if scene.m_是否开场景更新 :
		engineLib.flexCAPI.f_场景Update(ctypes.c_uint(scene.m_场景ID))
		engineLib.flexCAPI.f_场景Update(ctypes.c_uint(scene.m_场景ID))
	
	if scene.m_是否开启engine物理回传到Bl :
		for obj in engine_物体创建.g_动态物理体.keys() :
			print(obj.name, 'm_是否开启engine物理回传到Bl')
			pass




def register():
	bpy.types.Scene.m_场景ID = bpy.props.IntProperty(name='场景ID', min=0)
	bpy.types.Scene.m_PhysX = bpy.props.BoolProperty(default=False, name='PhysX')
	bpy.types.Scene.m_FleX = bpy.props.BoolProperty(default=False, name='FleX')

	bpy.types.Scene.m_FleX_粒子数 = bpy.props.IntProperty(default=65530, name='粒子数')
	bpy.types.Scene.m_FleX_飞溅数 = bpy.props.IntProperty(default=65530, name='飞溅数')



	bpy.types.Scene.m_是否开启物理模拟 = bpy.props.IntProperty(name='是否开启物理模拟', default=False)
	bpy.types.Scene.m_是否开场景更新 = bpy.props.BoolProperty(name='是否开场景更新', default=False)

	bpy.types.Scene.m_是否开启engine物理回传到Bl = bpy.props.IntProperty(name='物理回传到Bl', default=True)



	bpy.app.handlers.frame_change_pre.append(f_engine_场景更新)
	
	for c in classes:
		bpy.utils.register_class(c)

	#f_轮询场景(bpy.context.scene)


def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)
	

